using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Cards
{
    /// <summary>
    /// This is a game component that checks for the end of game conditions.
    /// </summary>
    /// 
    
    public class EndGame
    {

        //able to spider through rows on the board
        Spider endSpider;

        //Lists to hold chips included in rows of five
        List<int> HoroP1 = new List<int>();
        List<int> HoroP2 = new List<int>();
        //int[] HORO = new int[5];
        //int HoroChipNum = 0;

        List<int> VertP1 = new List<int>();
        List<int> VertP2 = new List<int>();

        //int[] VERT = new int[5];
        //int VertChipNum = 0;

        List<int> DiagL_RP1 = new List<int>();
        List<int> DiagL_RP2 = new List<int>();
        //int[] DIAGL_R = new int[5];
        //int DiagL_RChipNum = 0;

        List<int> DiagR_LP1 = new List<int>();
        List<int> DiagR_LP2 = new List<int>();
        //int[] DIAGR_L = new int[5];
        //int DiagR_LChipNum = 0;

        public Boolean[] foundHoro;
        public Boolean[] foundVert;
        public Boolean[] foundDiagL_R;
        public Boolean[] foundDiagR_L;

        //A dictionary of previous lists
        Dictionary<string, List<int>> usedChips;

        //Tells how many complete rows a user has
        public static int[] completeRows;

        public SoundEffect win;
        public SoundEffect lose;

        public static Boolean drawWin;
        public static Boolean drawLose;

        public EndGame(ContentManager Content)
        {
            // TODO: Construct any child components here     
            win = Content.Load<SoundEffect>
                ("Win");
            lose = Content.Load<SoundEffect>
                ("Lose");


        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            drawLose = false;
            drawWin = false;

            // TODO: Add your initialization code here
            foundHoro = new Boolean[2];
            foundVert = new Boolean[2];
            foundDiagL_R = new Boolean[2];
            foundDiagR_L = new Boolean[2];

            foundHoro[1] = false;
            foundVert[1] = false;
            foundDiagL_R[1] = false;
            foundDiagR_L[1] = false;

            foundHoro[0] = false;
            foundVert[0] = false;
            foundDiagL_R[0] = false;
            foundDiagR_L[0] = false; 


            //Horo.Add(1);

            usedChips = new Dictionary<string, List<int>>();

            usedChips.Add("HoroP1", HoroP1);
            usedChips.Add("HoroP2", HoroP2);

            usedChips.Add("VertP1", VertP1);
            usedChips.Add("VertP2", VertP2);

            usedChips.Add("DiagL_RP1", DiagL_RP1);
            usedChips.Add("DiagL_RP2", DiagL_RP2);

            usedChips.Add("DiagR_LP1", DiagR_LP1);
            usedChips.Add("DiagR_LP2", DiagR_LP2);

            endSpider = new Spider();
            completeRows = new int[2];


        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
        }

        public Boolean endCheck(bool isPlayer1)
        {
            //HoroChipNum = 0;
            //VertChipNum = 0;
            //DiagL_RChipNum = 0;
            //DiagR_LChipNum = 0;
            //clear out lists of chips that are in rows of five


            

            int currentPlayer;
            if (isPlayer1)
            {
                currentPlayer = 1;
            }
            else
                currentPlayer = 2;

            //usedChips["Vert" + currentPlayer.ToString()].Clear();


            for (int i = 1; i <= 100; i++)
            {
                if (Board.chips[i] == (Board.chipState)currentPlayer)
                {
                    endSpider.ckUp(usedChips["VertP" + currentPlayer.ToString()], i, currentPlayer);
                    if(usedChips["VertP1"].Count >= 5)
                    {
                        //player one wins
                        win.Play(0.2f,0.0f, 0.0f);
                        drawWin = true;
                        
                    }
                    if (usedChips["VertP2"].Count >= 5)
                    {
                        lose.Play(0.2f, 0.0f, 0.0f);
                        drawLose = true;
                    }
                    //endSpider.ckDn(usedChips["VertP" + currentPlayer.ToString()], i, currentPlayer);
                    //if (usedChips["VertP1"].Count >= 5)
                    //{
                    //    //player one wins
                    //}
                    //if (usedChips["VertP2"].Count >= 5)
                    //{
                    //    //player two wins
                    //}


                    endSpider.ckL(usedChips["HoroP" + currentPlayer.ToString()], i, currentPlayer);
                    if (usedChips["HoroP1"].Count >= 5)
                    {
                        //player one wins
                        win.Play(0.2f, 0.0f, 0.0f);
                        drawWin = true;
                    }
                    if (usedChips["HoroP2"].Count >= 5)
                    {
                        lose.Play(0.2f, 0.0f, 0.0f);
                        drawLose = true;
                    }
                    //endSpider.ckR(usedChips["HoroP" + currentPlayer.ToString()], i, currentPlayer);
                    //if (usedChips["HoroP1"].Count >= 5)
                    //{
                    //    //player one wins
                    //}
                    //if (usedChips["HoroP2"].Count >= 5)
                    //{
                    //    //player two wins
                    //}

                    endSpider.ckTL(usedChips["DiagL_RP" + currentPlayer.ToString()], i, currentPlayer);
                    if (usedChips["DiagL_RP1"].Count >= 5)
                    {
                        //player one wins
                        win.Play(0.2f, 0.0f, 0.0f);
                        drawWin = true;
                    }
                    if (usedChips["DiagL_RP2"].Count >= 5)
                    {
                        lose.Play(0.2f, 0.0f, 0.0f);
                        drawLose = true;
                    }
                    //endSpider.ckBR(usedChips["DiagL_TP" + currentPlayer.ToString()], i, currentPlayer);
                    //if (usedChips["DiagL_TP"].Count >= 5)
                    //{
                    //    //player one wins
                    //}
                    //if (usedChips["DiagL_TP"].Count >= 5)
                    //{
                    //    //player two wins
                    //}


                    endSpider.ckBL(usedChips["DiagR_LP" + currentPlayer.ToString()], i, currentPlayer);
                    if (usedChips["DiagR_LP1"].Count >= 5)
                    {
                        //player one wins
                        win.Play(0.2f, 0.0f, 0.0f);
                        drawWin = true;
                    }
                    if (usedChips["DiagR_LP2"].Count >= 5)
                    {
                        lose.Play(0.2f, 0.0f, 0.0f);
                        drawLose = true;
                    }
                    //endSpider.ckBL(usedChips["DiagR_LP" + currentPlayer.ToString()], i, currentPlayer);
                    //if (usedChips["DiagR_LP"].Count >= 5)
                    //{
                    //    //player one wins
                    //}
                    //if (usedChips["DiagR_LP"].Count >= 5)
                    //{
                    //    //player two wins
                    //}
                }

                //if (completeRows[currentPlayer - 1] == 2)
                //{
                       //WINNER!
                //}
                //completeRows[currentPlayer - 1] = 0;


                    //ckL(usedChips["Horo"]);
                    //ckR(usedChips["Horo"]);


                    //ckTL(usedChips["DiagL_R"]);
                    //ckBR(usedChips["DiagL_R"]);


                    //ckTR(usedChips["DiagR_L"]);
                    //ckBL(usedChips["DiagR_L"]);


            }

            //if (HoroChipNum == 5)
            //{
            //    for (int i = 0; i <= 5; i++)
            //    {
            //        Horo.Add(HORO[i]);
            //    }
            //}

            return false;
        }

    }
}